package com.wiseman.writing;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CharsAdapter extends BaseAdapter implements OnItemClickListener, ConstValue{

	private Context mContext;

	private String mStrChars;
	private LayoutInflater mInflater;
	boolean mIsMini = false;

	public CharsAdapter(Context c, String chars, boolean isMini) {
		super();
		this.mContext = c;
		this.mStrChars = chars;
		mIsMini = isMini;
		mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void updateData(String chars) {
		mStrChars = chars;
		notifyDataSetChanged();
	}

	public int getCount() {
		return mStrChars.length();
	}

	@Override
	public Object getItem(int index) {

		return mStrChars.charAt(index);
	}

	@Override
	public long getItemId(int index) {
		return index;
	}

	@Override
	public View getView(int index, View convertView, ViewGroup parent) {
		TextView tvChar = null;

		if (convertView == null) {
			convertView = mInflater.inflate(mIsMini?R.layout.griditem_min:R.layout.griditem, null);
		}

		tvChar = (TextView) convertView.findViewById(R.id.tvChar);
		tvChar.setText(mStrChars.substring(index, index + 1));

		return convertView;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		char c = mStrChars.charAt(position) ;

		Intent intent = new Intent();
		intent.putExtra(INTENTNAME_CHARCODE, c);
		intent.setClass(mContext, StrokeActivity.class);
		intent.putExtra(INTENTNAME_CHARLIST, mStrChars);
		intent.putExtra(INTENTNAME_CHARINDEX, position);

		mContext.startActivity(intent);
		
	}

}
